import { RouteRecordRaw } from 'vue-router'
const modules = import.meta.glob('../views/test/*/*.vue')
// console.log(modules)

interface routerType {
  name: string;
  redirect?: string;
  path: string;
  meta?: {
    required?: boolean;
    title?: string;
    keepAlive?: boolean;
  };
  component: string;
  children?: Array<routerType>;
}

export const addRoute = (routers: Array<routerType>): Array<RouteRecordRaw> => {
  let arr: Array<RouteRecordRaw> = []
  routers.filter(itemRouter => {
    let child: Array<RouteRecordRaw> = []
    // 是否存在子集
    if (itemRouter.children && itemRouter.children.length) {
      child = addRoute(itemRouter.children)
    }
    arr.push({
      name: itemRouter.name,
      redirect: itemRouter.redirect,
      path: itemRouter.path,
      meta: itemRouter.meta,
      // component: () => import(`@v/${itemRouter.component}`),
      component: modules[`../views${itemRouter.component}`],
      children: child
    })
  })
  return arr
}